﻿using MySql.Data.MySqlClient;
using MySql.Data;
using System.Data;
using System.Configuration;
using System.Windows.Forms;
using System.Text;

namespace MyDns
{
    public class MySqlDBHelper
    {

        private static string dbConnectionString = MySqlConn;
        private MySqlConnection connection;

        public MySqlDBHelper()
        {
            this.connection = new MySqlConnection(MySqlDBHelper.dbConnectionString);
        }
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
        private static string MySqlConn
        {
            get
            {
                string iniPath = Application.StartupPath + "\\mysqlconn.ini";
                StringBuilder server=new StringBuilder(100);
                StringBuilder port = new StringBuilder(10);
                StringBuilder database = new StringBuilder(10);
                StringBuilder uid = new StringBuilder(10);
                StringBuilder pwd = new StringBuilder(20);
                GetPrivateProfileString("mysqlconn", "server", "", server, 100, iniPath);
                GetPrivateProfileString("mysqlconn", "port", "", port, 10, iniPath);
                GetPrivateProfileString("mysqlconn", "database", "", database, 10, iniPath);
                GetPrivateProfileString("mysqlconn", "uid", "", uid, 10, iniPath);
                GetPrivateProfileString("mysqlconn", "pwd", "", pwd, 20, iniPath);
                string ret = "server=" + server + ";port=" + port + ";database=" + database + ";uid=" + uid + ";pwd=" + pwd;
                return ret;
            }
        }

        public MySqlDBHelper(string connString)
        {
            this.connection = new MySqlConnection(connString);
        }

        public MySqlCommand GetSqlStringCommond(string sqlQuery)
        {
            MySqlCommand dbCommand = connection.CreateCommand();
            dbCommand.CommandText = sqlQuery;
            dbCommand.CommandType = CommandType.Text;
            return dbCommand;
        }
        public DataSet ExecuteDataSet(MySqlCommand cmd)
        {
            MySqlDataAdapter dbDataAdapter = new MySqlDataAdapter();
            dbDataAdapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            dbDataAdapter.Fill(ds);
            return ds;
        }

        public DataTable ExecuteDataTable(MySqlCommand cmd)
        {
            MySqlDataAdapter dbDataAdapter = new MySqlDataAdapter();
            dbDataAdapter.SelectCommand = cmd;
            DataTable dataTable = new DataTable();
            dbDataAdapter.Fill(dataTable);
            return dataTable;
        }

        public MySqlDataReader ExecuteReader(MySqlCommand cmd)
        {
            cmd.Connection.Open();
            MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

        public int ExecuteNonQuery(MySqlCommand cmd)
        {
            cmd.Connection.Open();
            int ret = cmd.ExecuteNonQuery();
            cmd.Connection.Close();
            return ret;
        }

        public object ExecuteScalar(MySqlCommand cmd)
        {
            cmd.Connection.Open();
            object ret = cmd.ExecuteScalar();
            cmd.Connection.Close();
            return ret;
        }
    }
}
